package cn.icyzx.util;

import cn.icyzx.models.Work;
import net.sf.json.JSONObject;

import javax.servlet.http.Cookie;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;

/**
 * @Author zk
 * @Date 2017/2/6.
 * @Email 2117251154@qq.com
 */
public class CommonUtil {

    public static Map<String, String> cookiesToMap(Cookie[] cookies) {
        Map<String, String> map = new HashMap<String, String>();

        for (Cookie cookie : cookies) {
            map.put(cookie.getName(), cookie.getValue());
        }
        return map;
    }

    public static Cookie selectConditon(Cookie[] cookies) {
        if (null == cookies)
            return null;
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(StaticConstUtil.COOKIE_SELECT_CONDITION))
                return cookie;
        }
        return null;
    }

    public static String getTimeAgo(long beforeTime) {
        Date date = new Date();
        long time = (date.getTime() - beforeTime) / 1000;
        if (time < 60) return "刚刚";
        else if (time / 60 / 60 < 1) return time / 60 + "分钟之前";
        else if (time / 60 / 60 / 24 < 1) return time / 60 / 60 + "个小时之前";
        else if (time / 60 / 60 / 24 / 30 < 1) return time / 60 / 60 / 24 + "天之前";
        else if (time / 60 / 60 / 24 / 30 / 12 < 1) return time / 60 / 60 / 24 / 30 + "个月之前";
        else return time / 60 / 60 / 24 / 30 / 12 + "年之前";
    }

    /*** 使多行字符串转成一行字符串
     *
     * @param s1
     * @return 规范化的单行字符串
     */
    public static String formateStringToLine(String s1) {
        StringBuffer s2 = new StringBuffer(s1);
        return s2.toString();
    }

    public static class WorkOderByOverComparator implements Comparator<Work> {
        @Override
        public int compare(Work o1, Work o2) {
            return o1.getOverNumber() < o2.getOverNumber() ? 1 : -1;
        }
    }

    public static class WorkOderByRecommendComparator implements Comparator<Work> {
        @Override
        public int compare(Work o1, Work o2) {
            return o1.getRecommendNumber() < o2.getRecommendNumber() ? 1 : -1;
        }
    }

    public static class WorkOderByCollectionComparator implements Comparator<Work> {
        @Override
        public int compare(Work o1, Work o2) {
            return o1.getCollectionNumber() < o2.getCollectionNumber() ? 1 : -1;
        }
    }

    /*** 拼装url的条件
     *
     * @param condition
     * @return
     */
    public static String conditionToUrl(Map<String, String> condition) {
        condition.remove("curPage");
        StringBuffer stringBuffer = new StringBuffer("");
        Set<String> keySet = condition.keySet();
        for (String key : keySet) {
            if (condition.get(key) != null) {
                if (stringBuffer.toString().equals(""))
                    stringBuffer.append("?");
                else
                    stringBuffer.append("&" + key + "=" + condition.get(key));
            }

        }
        return stringBuffer.toString();
    }

    public static <T> JSONObject beanToJsonObject(T t) {
        JSONObject jsonObject = new JSONObject();
        Class<?> clazz = t.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            String getMethod = "get" + field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1, field.getName().length());
            try {
                Method method = clazz.getMethod(getMethod);
                jsonObject.put(field.getName(), method.invoke(t, null));
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("bean to jsonObject error");
            }
        }

        return jsonObject;
    }

}
